package com.hema.auction.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.hema.auction.R;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 绑定手机号
 * author by john
 */

public class BindPhoneActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    private String code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_phone);
        ButterKnife.bind(this);
    }

    /**
     * 获取验证码
     */
    private void getCode() {
        if (Utils.isEmpty(etPhone.getText().toString())) {
            showToastSafe(R.string.phone_isempty);
        } else if (!Utils.isValidPhone(etPhone.getText().toString())) {
            showToastSafe(R.string.phone_error);
        } else {
            showProgressDialog(R.string.login_now);
            HttpManager.getInstance(this).getCode(etPhone.getText().toString(), this);
        }
    }

    /**
     * 绑定手机号
     */
    private void bd() {
        if (Utils.isEmpty(etPhone.getText().toString())) {
            showToastSafe(R.string.phone_isempty);
        } else if (!Utils.isValidPhone(etPhone.getText().toString())) {
            showToastSafe(R.string.phone_error);
        } else if (Utils.isEmpty(etCode.getText().toString())) {
            showToastSafe(R.string.verification_code_isempty);
        } else {
            showProgressDialog();
            HttpManager.getInstance(this).bdPhone(etPhone.getText().toString(), etCode.getText().toString(), this);
        }
    }

    @OnClick({R.id.tv_send_code, R.id.tv_bind})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_send_code:
                getCode();
                break;
            case R.id.tv_bind:
                bd();
                break;
        }
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_SEND_CODE:
                try {
                    if (result.getInt("errcode") == 0) {
                        code = result.getString("data");
                    }
                    showToastSafe(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case TAG_BD_PHONE:
                try {
                    if (result.getInt("errcode") == 0) {
                        profileInfo.setPhone(etPhone.getText().toString());
                        updateProfile();
                        finish();
                    }
                    showToastSafe(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
